define(['jquery', 'bootstrap', 'backend', 'table', 'form','upyun','summernote'], function ($, undefined, Backend, Table, Form,UpYun,Summernote) {

    var Controller = {
        index: function () {
            var table = $("#table");
            $(document).on('click', '.thisaddtabs', function (e) {
                Backend.api.addtabs(Backend.api.replaceids(this, $(this).attr('url')), $(this).attr("title"));
                return false;
            });
            $(document).on('click', '#chkSelectAll', function (e) {
                $(this).prop("checked","checked");
                $("#table input[name=btSelectItem]").each(function(){
                    $(this).prop("checked",!$(this).prop("checked"));
                });
                return false;
            });
            $(document).on('click', 'input[name=btSelectItem]',function (e) {
                if($("input[name=btSelectItem]:checked").length>0){
                    $(".btn-edit").removeClass('disabled');
                    if(typeof($(".btn-more"))!='undefined'){
                        $(".btn-more").removeClass('disabled');
                    }
                }else{
                    $(".btn-edit").addClass('disabled');
                    if(typeof($(".btn-more"))!='undefined'){
                        $(".btn-more").addClass('disabled');
                    }
                }
            });
            $(".btn-edit").on("click",function(e){
                e.preventDefault();
                var chenked = $("input[name='btSelectItem']:checked");
                Backend.api.addtabs(Backend.api.replaceids(this, 'column/edit?ids='+chenked[0].value),'编辑');
                return false;
            });
            $(".btn-del").on("click",function (e) {
                e.preventDefault();
                var chenked = $("input[name='btSelectItem']:checked");
                var ids = "";
                for(var i=0;i<chenked.length;i++){
                    ids += chenked[i].value+",";
                }
                if(chenked.length==0){
                    var index2 = Layer.confirm('请选择类别','请选择');

                }else{
                    var id = $(this).data("id");
                    var that = this;
                    var index = Layer.confirm(
                        __('Are you sure you want to delete this item?'),
                        {icon: 3, title: __('Warning'), shadeClose: true},
                        function () {
                            $.ajax({
                                type: "post",
                                url: "/admin/category/del.html",
                                data: {ids:ids},
                                dataType: 'JSON',
                                success: function (data, status) {
                                    if (status == "success") {
                                        Layer.close(index);
                                        location.reload();
                                    }
                                },
                                error: function () {
                                    //alert('编辑失败');
                                },
                                complete: function () {

                                }
                            });
                        }
                    );
                }

            });
            $(".btn-delone").on("click",function(e){
                var ids = $(this).prop("title");
                var index = Layer.confirm(
                    __('Are you sure you want to delete this item?'),
                    {icon: 3, title: __('Warning'), shadeClose: true},
                    function () {
                        $.ajax({
                            type: "post",
                            url: "/admin/category/del.html",
                            data: {ids:ids},
                            dataType: 'JSON',
                            success: function (data, status) {
                                if (status == "success") {
                                    Layer.close(index);
                                    location.reload();
                                }
                            },
                            error: function () {
                                //alert('编辑失败');
                            },
                            complete: function () {

                            }
                        });
                    }
                );
            });
            //表格切换
            /*
            //窗体太窄，自动缩小。
            if($(window).width()<768){
                $("#toggleview").trigger('click');
            }
            $(window).resize(function(){
                if($(window).width()<=768){
                    if(!$("#table thead").is(":hidden")){
                      $("#toggleview").trigger('click');
                    }
                }
                if($(window).width()>768){
                    if($("#table thead").is(":hidden")){
                      $("#toggleview").trigger('click');
                    }
                }
            });
            */
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(".returnback").click(function(){
                history.back();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
